package com.qiangesoft.rabbitmq.mode.work;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 工作模式
 * ps:多个消费者竞争消息
 *
 * @author qiangesoft
 * @date 2024-05-08
 */
@Slf4j
@RequestMapping("/work")
@RestController
public class WorkMode {

    private final String QUEUE = "work.queue";

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 生产者
     */
    @GetMapping("/send")
    public void send(String message) {
        for (int i = 0; i < 10; i++) {
            rabbitTemplate.convertAndSend(QUEUE, message + i);
        }
    }

    /**
     * 消费者1
     */
    @RabbitListener(queuesToDeclare = @Queue(name = QUEUE))
    public void receiveMessage1(String message) {
        log.info("Received Message by consumer1: " + message);
    }

    /**
     * 消费者2
     */
    @RabbitListener(queuesToDeclare = @Queue(name = QUEUE))
    public void receiveMessage2(String message) {
        log.info("Received Message by consumer2: " + message);
    }

}
